package com.probiz.estore.content.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.common.model.content.AdPositionType;
import com.probiz.estore.content.dao.AdPositionTypeDao;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;

public class AdPositionTypeDaoImpl extends HibernateGenericDaoImpl<AdPositionType> implements AdPositionTypeDao {
	
	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "select s from AdPositionType s order by s.adPositionTypeId asc");
	}
	
	public AdPositionType getAdPositionTypeByCode(String positionCode){
		return (AdPositionType)this.findUnique("from AdPositionType where positionCode = ?", positionCode);
	}
	
	public boolean isExistPositionCode(final String positionCode, final Integer positionId) {
		
		Long size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer buff = new StringBuffer();
				buff.append("select count(*) from AdPositionType s where s.positionCode = ? ");
				if(positionId!=null){
					buff.append(" and s.adPositionTypeId != ?");
				}
				Query query = session.createQuery(buff.toString());
				query.setString(0, positionCode);
				if(positionId!=null){
					query.setInteger(1, positionId);
				}
				Long rows =  (Long)query.uniqueResult();
				return rows;
			}

		});
		return size>0?true:false;
	}

}